package com.sinosoft.sm.system.common.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AjaxFilter implements Filter {

	private static final Logger logger = LoggerFactory.getLogger(AjaxFilter.class);

	public void init(FilterConfig filterConfig) throws ServletException {
		logger.debug("Ajax过滤器启动");
	}

	public void doFilter(ServletRequest servletRequestt, ServletResponse servletResponse,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequestt;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		String ajaxSubmit = request.getHeader("X-Requested-With");
		if (ajaxSubmit != null && ajaxSubmit.equals("XMLHttpRequest")) {
			if (request.getSession(false) == null) {
				response.setHeader("sessionstatus", "timeout");
				response.getWriter().print("sessionstatus");
				return;
			}
		}
		chain.doFilter(servletRequestt, servletResponse);
	}

	public void destroy() {
		logger.debug("Ajax过滤器销毁");
	}

}
